/**
 * www.jbarreto.com all rights reserved 2011
 */
package com.jbarreto.vc.actions;

import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.jbarreto.vc.dataobjects.Pelicula;
import com.jbarreto.vc.plugin.HibernatePlugIn;

/**
 * @author JBarreto
 * 
 *         jj_b_f@yahoo.com.mx
 * 
 *         14/01/2011 10:51:58
 */
public class ListarPeliculasAction extends Action {

	/*
	 * (non-Javadoc)
	 * 
	 * @seeorg.apache.struts.action.Action#execute(org.apache.struts.action.
	 * ActionMapping, org.apache.struts.action.ActionForm,
	 * javax.servlet.http.HttpServletRequest,
	 * javax.servlet.http.HttpServletResponse)
	 */
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		System.out.println("Getting session factory");
		/* Get the servlet context */
		ServletContext context = request.getSession().getServletContext();
		/* Retrieve Session Factory */
		SessionFactory _factory = (SessionFactory) context
				.getAttribute(HibernatePlugIn.SESSION_FACTORY_KEY);
		/* Open Hibernate Session */
		Session session = _factory.openSession();

		// Criteria Query Example
		Criteria crit = session.createCriteria(Pelicula.class);
		// crit.add(Restrictions.like("shortdesc", "%"
		// + formObj.getKeyword() +"%")); //Like condition

		// Fetch the result from database
		List<Pelicula> peliculas = crit.list();
		request.setAttribute("peliculas", peliculas);

		/* Close session */
		session.close();
		System.out.println("Hibernate Session Closed");

		return mapping.findForward("success");
	}

}
